var title = "客户信息";
var config = {
    url_page: _dir + "shareInfoList.html",
    url_page2: _dir + "userManager.html",
    url_add: _dir + "shareInfoList_add.html",
    title_page: title + "查询",
    title_list: title + "列表",
    title_add: "添加" + title,//商品
    title_edit: "修改" + title,//商品
    code_list: "/userManager/share/query",//商品
    code_delete: "/userManager/share/del",//商品
    code_add: "/userManager/share/add",
    code_edit: "/userManager/share/update",//商品
    code_dataUpload: "/book/coverUpload",//商品
    code_imgUpload: "/book/upload",//商品
    pagination_01: $("#page_01"),
    getQuDao: "/channel/query",
};
$(document).ready(function () {
    if (_page == "shareInfoList") {
        var _fid = $.trim($.request.queryString["fid"]);
        var _fstaffNo = $.trim($.request.queryString["id"]);
        var _rtype = $.trim($.request.queryString["rtype"]);
        var _msgId = 0;
        $('.d_add1').click(function () {
            console.log(_fid)
            location.href = '../tpl/shareInfoList_add.html?fid=' + _fid+'&id='+_fid+'&rtype=add'
        })
        var pageNo = 1;
        setTitle_01(config['title_list'], config['title_page'], config['url_add']);
       // setDefaultDate($("#startTime"),$("#endTime"))
        //加载事件
        var _trs, _pageSize, _pageCount;
        var setData = function (pIndex, pSize) {
            pIndex = pIndex || 1;
            pSize = pSize || 20;
            var pageSetBody = { "pageNo": pIndex, "pageSize": pSize };
            var sendObj = {
                "id": _fstaffNo
                // "name": $("#name").val(),
            };
            window.sessionStorage.setItem(_page+"data",JSON.stringify({
                'sendObj':sendObj,
                'pageSetBody':pageSetBody
            }));
            $.extend(sendObj,pageSetBody);
            _call(config['code_list'], sendObj, function (res) {
                _trs = "";
                if (!res.body || res.body.length == 0) {
                    _trs = "";
                    _pageSize = 1;
                    _pageCount = 1;
                    pageNo--
                    d_alert('警告','没有更多数据啦！','warning')
                }
                else {
                    _pageSize = res.body.pageSize;
                    _pageCount = res.body.count;
                    $.each(res.body, function (i, v) {
                        _trs = _trs + `<tr>
                            <td>${dataIsNull(v['id'])}</td>
                            <td style="display:none">${dataIsNull(v['channelId'])}</td>
                            <td>${dataIsNull(v['channelName'])}</td>

                            <td>${dataIsNull(v['state'])==0?'未付款':dataIsNull(v['state'])==1?'已付款':'交易成功'}</td>
                            <td>${dataIsNull(v['recipientTaobaoId'])}</td>
                            <td>${getTdOperate(6, config['url_add'], v.clientUserId, "id", v.id)}</td>
                        </tr>`;
                    });
                    setPageData(res.body);
                    $("#table_01 tbody").html(_trs);
                }

                //绑定删除事件
                setDelete("id", config['code_delete']);

                //分页方法
            });
        };

        var loadingAll = function () {
            //初始化加载数据
            setData();
        }

        autoSearchByCookie(loadingAll);
        //查询方法
        loadingAll();
        $(".homePage").click(function(){
            pageNo = 1
            setData(pageNo)
        })
        $(".prevPage").click(function(){
            if(pageNo > 1){
                pageNo--
                setData(pageNo)
            }else{
                d_alert("警告",'已经到头啦！','warning')
            }
            
        })
        $(".nextPage").click(function(){
            pageNo++
            setData(pageNo)
        })
        //查询方法
        setQuery(setData)
    }
    else if (_page == "shareInfoList_add") {

        var sendObj2 = {};
        $("#clientUserId").val($.trim($.request.queryString["id"]))
        //初始化
        var PageInit = function () {
            var _fid = $.trim($.request.queryString["fid"]);
            var _fstaffNo = $.trim($.request.queryString["id"]);
            var _rtype = $.trim($.request.queryString["rtype"]);
            var _msgId = 0;
            this.get_fid = function () {
                return this._fid;
            };
            this.get_msgId = function () {
                return this._msgId;
            };
            this.set_fid = function (v) {
                this._fid = v;
            }
            this.set_msgId = function (v) {
                this._msgId = v;
            }

            this.setDefault = function (v) {
                if ((_fid == "" && _fstaffNo == "" && _rtype == "")||_rtype=='add') {
                    //添加
                    this.set_msgId(config['code_add']);
                    this.set_fid(0);
                    setTitle_02(config['title_add'],config['url_page2']);
                    getQuDapAjax(config['getQuDao'],'#channelId',{},function () {
                        $('.select2').change()
                    })
                }

                // // 获取渠道公共方法
                // function getQuDapAjax(_code,dom,sendObj,fun) {
                //     var html=''
                //     _call(_code, sendObj,function (res) {
                //         $.each(res.body, function (i, v) {
                //             html+=`<option value="${v['id']}">${v['name']}</option>`
                //         })
                //         $(dom).html(html)
                //         if(fun){
                //             fun()
                //         }
                //     });
                //
                // }
                else if (_fid != "" && _fstaffNo != "" && _rtype == "edit") {
                    //修改
                    this.set_msgId(config['code_edit']);
                    this.set_fid(_fid);
                    setTitle_02(config['title_edit'],config['url_page2']);
                    sendObj2['id'] = _fstaffNo;

                    if (getPageData(_fstaffNo)) {

                        var _v = getPageData(_fstaffNo);
                        var formObj = new Form();
                        formObj.init(_v);
                        console.log(_v)
                        getQuDapAjax1(config['getQuDao'],'#channelId',{},_v.channelName,_v.channelId,function () {
                            $('.select2').change()
                        })
                    }
                }

            }
        };
        var _default = new PageInit();
        _default.setDefault();
        //提交事件
        $(".validate-form .submit").on("click", function () {
            var _this = $(this);
            // sendObj2={};
            if ($(".validate-form").validate().form()) {
               // sendObj2["id"] = $("#id").val();\

                sendObj2["channelId"] = $("#channelId").val();
                sendObj2["clientUserId"] = $("#clientUserId").val();
                sendObj2["state"] = $("#state").val();
                sendObj2["recipientTaobaoId"] = $("#recipientTaobaoId").val();
                _call(_default.get_msgId(), sendObj2, function (res) {
                    confirm_add_ok(res, config['url_page']+"?fid="+$.trim($.request.queryString["fid"])+"&id="+$.trim($.request.queryString["fid"]), function () {
                        window.location.href = window.location.href;
                    });
                });

            }
        });

    }
    if($(".select2").length > 0){
        $(".select2").select2();
    }
});